package cn.caidabao.gb.component.sip.server.request.send;

import cn.caidabao.gb.bean.GBSipRegisterSession;
import cn.caidabao.gb.bean.OGSipCallIdCallBack;
import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

public abstract class RequestSender<EXTRA,RESULT> {

    private class ResultObject{
        RESULT result;
        private void setResult(RESULT result){
            this.result = result;
        }
        private RESULT getResult(){
            return result;
        }
    }

    @SneakyThrows
    public RESULT sendSync(GBSipRegisterSession gbSipRegisterSession, EXTRA extraData){
        CountDownLatch countDownLatch = new CountDownLatch(1);
        OGSipCallIdCallBack ogSipCallBack = sendAsync(gbSipRegisterSession, extraData);
        ResultObject resultObject = new ResultObject();
        ogSipCallBack.callBack((re)->{
            resultObject.setResult((RESULT)re);
            countDownLatch.countDown();
        });
        countDownLatch.await();
        return resultObject.getResult();
    }
    abstract public OGSipCallIdCallBack<RESULT> sendAsync(GBSipRegisterSession gbSipRegisterSession, EXTRA extraData);
}
